package com.bwie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.ProdAttaMapper;
import com.bwie.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bwie.pojo.ProdAtta;
import com.bwie.service.ProdAttaService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
@Transactional
public class ProdAttaServiceImpl extends ServiceImpl<ProdAttaMapper, ProdAtta> implements ProdAttaService{
    @Autowired
    private ProdAttaMapper prodAttaMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public R addAttaList(ProdAtta prodAtta) {
        prodAttaMapper.delete(
                new QueryWrapper<ProdAtta>().lambda().eq(ProdAtta::getProdId,prodAtta.getAttaId())
        );
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        prodAtta.setCreateBy(userName);
        prodAtta.setCreateTime(new Date());
        prodAtta.setUpdateBy(userName);
        prodAtta.setUpdateTime(new Date());
        prodAttaMapper.insert(prodAtta);
        return R.OK();
    }
}
